#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
#define MAX 1000
struct PersonInfo{
    string name;
    int sex;
    int age;
    string address;
};
struct AddressBooks{
    struct PersonInfo person[MAX];
    int size;
};
void showMenu(){
    cout<<"**************************"<<endl;
    cout<<"*****  0:添加联系人  *****"<<endl;
    cout<<"*****  1:查看联系人  *****"<<endl;
    cout<<"*****  2:查询联系人  *****"<<endl;
    cout<<"*****  3:修改联系人  *****"<<endl;
    cout<<"*****  4:删除联系人  *****"<<endl;
    cout<<"*****  5:清空联系人  *****"<<endl;
    cout<<"*****  6:退出系统    *****"<<endl;
};
void addPerson(AddressBooks *abs){
    if (abs->size>1000){
        cout<<"通信录满了"<<endl;
        return;
    }
    cout<<"请输入姓名"<<endl;
    string name;
    cin>>name;
    abs->person[abs->size].name=name;
    
    cout<<"请输入性别"<<endl;
    int sex=0;
    while(true){
        cin>>sex;
        if (sex==1||sex==2){
            abs->person[abs->size].sex=sex;
            break;
        }else{
            cout<<"输入错误"<<endl;
        }
    }

    cout<<"请输入年纪"<<endl;
    int age=0;
    cin>>age;
    abs->person[abs->size].age=age;

    cout<<"请输入地址"<<endl;
    string address;
    cin>>address;
    abs->person[abs->size].address=address;
    abs->size++;
    cout<<"保存成功"<<endl;
    system("clear");
};

void showPersons(AddressBooks *abs){
    if (abs->size==0){
        cout<<"通讯录是空的"<<endl;
    }else{
        for(int i=0;i<abs->size;i++){
            cout<<"姓名: "<<abs->person[i].name<<"\t";
            cout<<"性别: "<<(abs->person[i].sex==1?"男":"女")<<"\t";
            cout<<"年纪: "<<abs->person[i].age<<"\t";
            cout<<"地址: "<<abs->person[i].address<<endl;
        }
    }
};

void searchPerson(AddressBooks *abs){
    string name;
    cout<<"请输入要查询的姓名"<<endl;
    cin>>name;
    for(int i=0;i<abs->size;i++){
        if (abs->person[i].name==name){
            cout<<"姓名: "<<abs->person[i].name<<"\t";
            cout<<"性别: "<<(abs->person[i].sex==1?"男":"女")<<"\t";
            cout<<"年纪: "<<abs->person[i].age<<"\t";
            cout<<"地址: "<<abs->person[i].address<<endl;
            break;
        }else{
            cout<<"查无此人";
            break;
        }
    }
};

int isExist(AddressBooks *abs,string name){
    for(int i=0;i<abs->size;i++){
        if (abs->person[i].name==name){
            return i;
        }
    }
    return -1;
};

void modifyPerson(AddressBooks *abs){
    string name;
    cout<<"请输入要修改的姓名"<<endl;
    cin>>name;
    int result=isExist(abs,name);
    if (result==-1){
        cout<<"查无此人"<<endl;
    }else{
        string name;
        cout<<"请输入姓名"<<endl;
        cin>>name;
        abs->person[result].name=name;
        
        int sex=0;
        while(true){
            cout<<"请输入性别"<<endl;
            cin>>sex;
            if (sex==1||sex==2){
                abs->person[result].sex=sex;
                break;
            }else{
                cout<<"输入错误"<<endl;
            }   
        }

        int age=0;
        cout<<"请输入年纪"<<endl;
        cin>>age;
        abs->person[result].age=age;
        
        string address;
        cout<<"请输入地址"<<endl;
        cin>>address;
        abs->person[result].address=address;

        cout<<"修改成功"<<endl;
    }
};

void deletePerson(AddressBooks *abs){
    cout<<"请输入要删除的联系人"<<endl;
    string name;
    cin>>name;
    int result=isExist(abs,name);
    if (result==-1){
        cout<<"查无此人"<<endl;
    }else{
        for(int i=result;i<abs->size;i++){
            abs->person[i]=abs->person[i+1];
            abs->size--;
        }   
    }
}

void clearPerson(AddressBooks *abs){
    abs->size=0;
};

int main(){
    struct AddressBooks abs;
    abs.size=0;
    int select=0;
    showMenu();
    while(true){
        cin>>select;
        switch(select){
            case 0:
                addPerson(&abs);
                break;
            case 1:
                showPersons(&abs);
                break;
            case 2:
                searchPerson(&abs);
                break;
            case 3:
                modifyPerson(&abs);
                break;
            case 4:
                deletePerson(&abs);
                break;
            case 5:
                clearPerson(&abs);
                break;
            case 6:
                exit(0);
                return 0;
                break;
            default:
                break;
        }
    }
    return 0;
}
